{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5069ae70",
   "metadata": {},
   "outputs": [],
   "source": [
    "#列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2c9bfd37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = list(range(10))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b724f56a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 7, 8, 9]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用列表解析查询大于5的数\n",
    "[i for i in a if i>5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c52f3625",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 7, 8, 9]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用filter\n",
    "list(filter(lambda x:x>5,a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0de2cc4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 80, 'b': 63, 'c': 81, 'd': 80, 'e': 77, 'f': 90}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字典\n",
    "from random import randint\n",
    "d={k:randint(60,90) for k in 'abcdef'}\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "64174fc3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 80, 'c': 81, 'd': 80, 'e': 77, 'f': 90}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用字典解析查询字典值大于70的键值\n",
    "{k:v for k,v in d.items() if v>70}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7ccd2978",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('a', 80), ('b', 63), ('c', 81), ('d', 80), ('e', 77), ('f', 90)])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用filter\n",
    "d.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "10ee4925",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 80, 'c': 81, 'd': 80, 'e': 77, 'f': 90}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict(filter(lambda x:x[1]>70,d.items()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "803ebc76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 15, 17}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#集合\n",
    "s = {randint(0,20) for _ in range(20)}\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "aec31317",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{6, 9, 12, 15}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用集合解析筛选能被3整除的数\n",
    "{i for i in s if i%3==0}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1b819ae1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{6, 9, 12, 15}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用filter\n",
    "set(filter(lambda x:x%3==0,s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d38a4fa1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
